package com.flot.yazb.home;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.flot.yazb.R;
import com.flot.yazb.base.BActivity;
import com.flot.yazb.base.BApplication;
import com.flot.yazb.base.model.BModel;
import com.flot.yazb.aop.stub.CodeStub;
import com.flot.yazb.model.other.SetModle;
import com.flot.yazb.home.moudle.FateFragment;
import com.flot.yazb.home.moudle.LineFragment;
import com.flot.yazb.home.moudle.MsgFragment;
import com.flot.yazb.home.moudle.NearFragment;
import com.flot.yazb.home.moudle.SettingFragment;
import com.flot.yazb.retrofit.service.LunchService;
import com.flot.yazb.widget.utils.NotificationsUtils;
import com.flot.yazb.widget.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class HomeActivity extends BActivity {
    @BindView(R.id.contain)
    FrameLayout contain;
    @BindView(R.id.icon_1)
    ImageView icon1;
    @BindView(R.id.title_1)
    TextView title1;
    @BindView(R.id.rlline)
    RelativeLayout rlline;
    @BindView(R.id.icon_2)
    ImageView icon2;
    @BindView(R.id.title_2)
    TextView title2;
    @BindView(R.id.rlfate)
    RelativeLayout rlfate;
    @BindView(R.id.icon_3)
    ImageView icon3;
    @BindView(R.id.title_3)
    TextView title3;
    @BindView(R.id.rlmsg)
    RelativeLayout rlmsg;
    @BindView(R.id.icon_4)
    ImageView icon4;
    @BindView(R.id.title_4)
    TextView title4;
    @BindView(R.id.rlnearly)
    RelativeLayout rlnearly;
    @BindView(R.id.icon_5)
    ImageView icon5;
    @BindView(R.id.title_5)
    TextView title5;
    @BindView(R.id.rlsetting)
    RelativeLayout rlsetting;
    @BindView(R.id.ivbig1)
    ImageView ivbig1;
    @BindView(R.id.ivbig2)
    ImageView ivbig2;
    @BindView(R.id.ivbig3)
    ImageView ivbig3;
    @BindView(R.id.ivbig4)
    ImageView ivbig4;
    @BindView(R.id.ivbig5)
    ImageView ivbig5;
    private LunchService mMyService;
    public ServiceConnection mServiceConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName name, IBinder service) {
            // TODO Auto-generated method stub
            mMyService = ((LunchService.MyBinder) service).getService();
            mMyService.sendNotice();
        }

        public void onServiceDisconnected(ComponentName name) {
            // TODO Auto-generated method stub
            startLunchService();
        }
    };


    private Fragment lf = LineFragment.newInstance(BApplication.getInstance().getUserModel());//直播fragment
    private Fragment ff = FateFragment.newInstance(BApplication.getInstance().getUserModel());//缘分fragment
    private Fragment mf = MsgFragment.newInstance(BApplication.getInstance().getUserModel());//消息fragment
    private Fragment nf = NearFragment.newInstance(BApplication.getInstance().getUserModel());//附近fragment
    private Fragment sf = SettingFragment.newInstance(BApplication.getInstance().getUserModel());//我的fragment
    private FragmentManager fm = getSupportFragmentManager();
    private String[] tags = new String[]{"LineFragment", "FateFragment", "MsgFragment", "NearFragment", "SettingFragment"};
    private Fragment selF;

    private void startLunchService() {
        Intent i = new Intent();
        i.setClass(HomeActivity.this, LunchService.class);
        bindService(i, mServiceConnection, BIND_AUTO_CREATE);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
        boolean isNiticeAble = NotificationsUtils.isNotificationEnabled(this);
        if (isNiticeAble == false) {
            ToastUtil.showToast(this,"您还未开启爱爱直播通知栏权限");
        }
        startLunchService();
        selF = lf;
        switchContent(new Fragment(), lf, tags[0]);

        SetModle.getSeeting(new BModel.Transit<SetModle>(this) {
            @Override
            public void onBorn(SetModle serializable, String code, String message) {
                if (code.equals(CodeStub.B_OK) && serializable != null) {
                    serializable.isSuccess = true;
                    BApplication.getInstance().setSetModel(serializable);
                }
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mServiceConnection != null)
            unbindService(mServiceConnection);
        mMyService.onDestroy();
        mMyService = null;
    }

    @OnClick({R.id.rlline, R.id.rlfate, R.id.rlmsg, R.id.rlnearly, R.id.rlsetting})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rlline:
                switchContent(selF, lf, tags[0]);
                selF = lf;
                updateStstus(true, icon1, title1, ivbig1);
                updateStstus(false, icon2, title2, ivbig2);
                updateStstus(false, icon3, title3, ivbig3);
                updateStstus(false, icon4, title4, ivbig4);
                updateStstus(false, icon5, title5, ivbig5);
                break;
            case R.id.rlfate:
                switchContent(selF, ff, tags[1]);
                selF = ff;
                updateStstus(false, icon1, title1, ivbig1);
                updateStstus(true, icon2, title2, ivbig2);
                updateStstus(false, icon3, title3, ivbig3);
                updateStstus(false, icon4, title4, ivbig4);
                updateStstus(false, icon5, title5, ivbig5);
                break;
            case R.id.rlmsg:
                switchContent(selF, mf, tags[2]);
                selF = mf;
                updateStstus(false, icon1, title1, ivbig1);
                updateStstus(false, icon2, title2, ivbig2);
                updateStstus(true, icon3, title3, ivbig3);
                updateStstus(false, icon4, title4, ivbig4);
                updateStstus(false, icon5, title5, ivbig5);
                break;
            case R.id.rlnearly:
                switchContent(selF, nf, tags[3]);
                selF = nf;
                updateStstus(false, icon1, title1, ivbig1);
                updateStstus(false, icon2, title2, ivbig2);
                updateStstus(false, icon3, title3, ivbig3);
                updateStstus(true, icon4, title4, ivbig4);
                updateStstus(false, icon5, title5, ivbig5);
                break;
            case R.id.rlsetting:
                switchContent(selF, sf, tags[4]);
                selF = sf;
                updateStstus(false, icon1, title1, ivbig1);
                updateStstus(false, icon2, title2, ivbig2);
                updateStstus(false, icon3, title3, ivbig3);
                updateStstus(false, icon4, title4, ivbig4);
                updateStstus(true, icon5, title5, ivbig5);
                break;
        }

    }

    public void updateStstus(boolean b, View view1, TextView view2, View view3) {
        int visable;
        if (b) {
            visable = View.GONE;
            view3.setVisibility(View.GONE);
            view2.setTextColor(getResources().getColor(R.color.red));
        } else {
            visable = View.VISIBLE;
            view3.setVisibility(View.GONE);
            view2.setTextColor(getResources().getColor(R.color.gray_text));
        }
    }

    /**
     * fragment 切换
     *
     * @param from
     * @param to
     */
    private Fragment mContent;//记录选中的fragment

    public void switchContent(Fragment from, Fragment to, String tag) {
        if (mContent != to) {
            mContent = to;
            FragmentTransaction transaction = fm.beginTransaction();
            if (!to.isAdded()) { // 先判断是否被add过
                transaction.hide(from)
                        .add(R.id.contain, to, tag).commit(); // 隐藏当前的fragment，add下一个到Activity中
            } else {
                transaction.hide(from).show(to).commit(); // 隐藏当前的fragment，显示下一个
            }
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1) {
            if (!NotificationsUtils.isNotificationEnabled(this)) {
                Toast.makeText(HomeActivity.this, "权限授予失败，无法接收通知", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(HomeActivity.this, "权限授予成功！", Toast.LENGTH_SHORT).show();
            }

        }
    }
}
